package com.example.redisdemo.controller;

import com.example.redisdemo.accessLimit.annotation.AccessLimit;
import com.example.redisdemo.apiIdempotent.annotation.ApiIdempotent;
import com.example.redisdemo.apiIdempotent.service.TokenService;
import com.example.redisdemo.util.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 * stringRedisTemplate API（部分展示）
 * opsForHash --> hash操作
 * opsForList --> list操作
 * opsForSet --> set操作
 * opsForValue --> string操作
 * opsForZSet --> Zset操作
 *
 * @ClassName TeController
 * @Author xiaojiang
 * @Date 2021/9/24 上午 11:11
 */
@RestController
public class TeController {

    @Autowired
    private TokenService tokenService;
    @Resource
    private StringRedisTemplate stringTemplate;

    /**
     * TODO Spring对RedisTemplate<K,V>类的默认配置(了解即可)
     *
     * @return HttpResult
     * @Param : request
     * @Param : response
     * @Author 江
     * @Date 2021/9/24 下午 13:55
     */
    @GetMapping("/test")
    public HttpResult selectAllPriceList(HttpServletRequest request, HttpServletResponse response) {

        ValueOperations<String, String> valueTemplate = stringTemplate.opsForValue();

//        valueTemplate.set("StringKey1", "hello spring boot redis, String Redis");
//        设置过期时间    表示20秒过期，可以设置时间的计数单位，有分，小时，年，月，日等
        valueTemplate.set("StringKey1", "hello spring boot redis, String Redis",20, TimeUnit.SECONDS);
        String value = valueTemplate.get("StringKey1");

        System.out.println(value);


//        StringRedisTemplate，即RedisTemplate<String, String>
//　　    key和value都是String。当需要存储实体类时，需要先转为String，再存入Redis。一般转为Json格式的字符串，所以使用StringRedisTemplate，需要手动将实体类转为Json格式。如

//        Gson gson = new Gson();
//        valueTemplate.set("StringKey2", gson.toJson(new Person("theName", 11)));
//        Person person = gson.fromJson(valueTemplate.get("StringKey2"), Person.class);
//        System.out.println(person);
        return HttpResult.newSuccess().add("value", value);
    }

    /**
     * @Description         接口防刷限流
     * @return HttpResult
     * @Author 江
     * @Date 2022/7/18 16:17
     */

    @GetMapping("accessLimit")
    @AccessLimit(maxCount = 6, seconds = 6)
    public HttpResult accessLimit(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("accessLimit!");
        return HttpResult.newSuccess();
    }

    @GetMapping("testaccessLimit")
    public HttpResult TestAccessLimit(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("TestaccessLimit!");
        ValueOperations<String, String> valueTemplate = stringTemplate.opsForValue();

        valueTemplate.set("tes", "hello spring boot redis, String Redis");
        String value = valueTemplate.get("tes");
        System.out.println("value = " + value);

        //String s = jedisUtil.get("tests");
        //System.out.println("Redis 取值为 ： " + s);
        return HttpResult.newSuccess();
    }

    @ApiIdempotent
    @GetMapping("apiIdempotent")
    public HttpResult apiIdempotent(HttpServletRequest request, HttpServletResponse response) {

        return HttpResult.newSuccess();
    }

    @GetMapping("createToken")
    public HttpResult createToken(HttpServletRequest request, HttpServletResponse response) {

        return tokenService.createToken();
    }

}
